#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;

const int N = 1e6;

unordered_map<int, int> belo, nbelo;
int n, q, total;
int p[N];

int find(int x){
    if(x != p[x]){
        p[x] = find(p[x]);
    }

    return p[x];
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> q;
    total = n;
    for(int i = 1; i <= n; i ++) {
        p[i] = i;
        belo[i] = i;
        nbelo[i] = i;
    }

    int op, x, y;
    while(q --){
        cin >> op;

        if(op == 1){
            cin >> x >> y;

            int a = belo[x], b = belo[y];
            if(a == 0 && b == 0) {
                continue;
            }else if(a == 0 && b != 0){
                belo[x] = b;
                nbelo[p[b]] = x;
                belo[y] = 0;
            }else if (a != 0 && b == 0){
                continue;
            }else{
                p[b] = a;
                belo[y] = 0;
            }
        }else if(op == 2){
            cin >> x;
            total ++;

            int a = belo[x];
            if(a == 0){
                p[total] = total;
                belo[x] = total;
                nbelo[total] = x;
            }else{
                p[total] = a;
            }
        }else{
            cin >> x;
            cout << nbelo[find(x)] << '\n';
        }

        // for(int i = 1; i <= total; i ++) cout << p[i] << " ";
        // cout << '\n';
    }

    return 0;
}